package com.ruoyi.ems.utils;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author yaohuaipeng
 * @date 2018/10/26-16:16  EMS202312111454446767
 */
public class OrderCodeUtils {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    private static final AtomicLong sequenceNumber = new AtomicLong(0);
    /*随机字符串生成*/
    public static String getComplexRandomString(int length) {
//        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String str = "EMS";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String time = sdf.format(new Date());

        Random random = new Random();
        StringBuffer sb = new StringBuffer();

        sb.append(time);

        for (int i = 0; i < length; i++) {
            int number = random.nextInt(4);
            System.out.println(number);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String generateOrderNumber() {
        String str = "EMS";
        long timestamp = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestampStr = dateFormat.format(new Date(timestamp));
        Random random = new Random();
        int randomNumber = random.nextInt(9999);
        System.out.println(randomNumber);
        String orderNumber = str + timestampStr + String.format("%04d", randomNumber);
        return orderNumber;
    }

    /*元转换为分*/
    public static  Integer unitConversionMinute(BigDecimal num){
        return Integer.valueOf(num.multiply(new BigDecimal("100")).setScale(0, BigDecimal.ROUND_DOWN).toString());
    }
    /*分转换为元*/
    public static  BigDecimal unitConversionElement(String num){
        return  new BigDecimal(num).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN);
    }

    public static void main(String[] args) {
        System.out.println(generateOrderNumber());
    }

}
